package cn.ltgodm.template.web.controller.user;

import cn.ltgodm.template.common.annotation.Log;
import cn.ltgodm.template.common.constant.UserConstants;
import cn.ltgodm.template.common.domain.AjaxResult;
import cn.ltgodm.template.common.domain.vo.DeptVO;
import cn.ltgodm.template.common.enums.BusinessType;
import cn.ltgodm.template.user.domain.param.DeptParam;
import cn.ltgodm.template.user.domain.param.DeptQueryParam;
import cn.ltgodm.template.user.service.DeptService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author ltgodm
 * @date 2024-03-27 14:05:08
 * @ 部门信息
 */
@RestController
@Tag(name = "部门管理")
@RequestMapping("/user/dept")
@ApiSupport(author = "ltgodm")
public class DeptController {
    @Resource
    private DeptService deptService;

    @GetMapping("/list")
    @Operation(summary = "分页获取部门信息")
    @PreAuthorize("hasAuthority('system:dept:query')")
    public IPage<DeptVO> list(@ParameterObject DeptQueryParam deptQueryParam) {
        return deptService.getDeptList(deptQueryParam);
    }

    @GetMapping("/list/exclude/{deptId}")
    @Operation(summary = "查询部门列表（排除节点）")
    @PreAuthorize("hasAuthority('system:dept:query')")
    public AjaxResult excludeChild(@PathVariable Long deptId) {
        return AjaxResult.success(deptService.getDeptListExcludeChild(deptId));
    }

    @GetMapping("/{deptId}")
    @Operation(summary = "根据部门编号获取详细信息")
    @PreAuthorize("hasAuthority('system:dept:query')")
    public AjaxResult getInfo(@PathVariable Long deptId) {
        return AjaxResult.success(deptService.getDeptById(deptId));
    }

    @PostMapping
    @Operation(summary = "新增部门")
    @PreAuthorize("hasAuthority('system:dept:add')")
    @Log(title = "部门管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody DeptParam deptParam) {
        if (!deptService.checkDeptNameUnique(deptParam)) {
            return AjaxResult.error("新增部门'" + deptParam.getDeptName() + "'失败，部门名称已存在");
        }
        int row = deptService.insertDept(deptParam);
        if (row > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    @PutMapping
    @Operation(summary = "修改部门")
    @PreAuthorize("hasAuthority('system:dept:edit')")
    @Log(title = "部门管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody DeptParam deptParam) {

        deptService.checkDeptDataScope(deptParam.getDeptId());

        if (!deptService.checkDeptNameUnique(deptParam)) {
            return AjaxResult.error("修改部门'" + deptParam.getDeptName() + "'失败，部门名称已存在");
        }
        if (deptParam.getParentId().equals(deptParam.getDeptId())) {
            return AjaxResult.error("修改部门'" + deptParam.getDeptName() + "'失败，上级部门不能是自己");
        }
        if (UserConstants.DEPT_DISABLE.equals(deptParam.getStatus()) && deptService.selectNormalChildrenDeptById(deptParam.getDeptId()) > 0) {
            return AjaxResult.error("该部门包含未停用部门");
        }

        int row = deptService.updateDept(deptParam);
        if (row > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    @DeleteMapping("/{deptId}")
    @Operation(summary = "删除部门")
    @PreAuthorize("hasAuthority('system:dept:remove')")
    @Log(title = "部门管理", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable Long deptId) {
        if (deptService.hasChildByDeptId(deptId)) {
            return AjaxResult.error("存在下级部门,不允许删除");
        }
        if (deptService.checkDeptExistUser(deptId)) {
            return AjaxResult.error("部门存在用户,不允许删除");
        }

        deptService.checkDeptDataScope(deptId);
        int row = deptService.deleteDeptById(deptId);
        if (row > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

}
